//
//  LKStatusViewModel.swift
//  新浪微博
//
//  Created by 胡不归是大虾 on 16/9/27.
//  Copyright © 2016年 胡不归是大虾. All rights reserved.
//

import UIKit

class LKStatusViewModel: NSObject {
    //规定好cell需要的数据
    ///头像图片URL
    var iconURL : URL?
    ///认证图标
    var avatarImage :UIImage?
    var vipImage :UIImage?
    var comment_text: String?
    var repost_text:String?
    var like_text:String?
    var repostContentText : String?
    var pic_urls: [LKPic_urls]? {
        return status?.retweeted_status == nil ? status?.pic_urls :
            status?.retweeted_status?.pic_urls
    }
    //来源标签
    var sourceText: String?
    //时间标签
    var timeText: String? {
        let dateString = status?.created_at ?? ""
        //设置日期格式
        sharedDateFormater.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        // 设置本地化信息
        sharedDateFormater.locale = Locale(identifier: "en")
        //转化为本地对象
        let creatDate = sharedDateFormater.date(from: dateString)
        //如果创建的日期转换不成工就直接return一个空串
        guard let newDate = creatDate else {
            return ""
        }
        
        //日历对象 提供了非常丰富的日期判断方法
        let calendar = Calendar.current
        //判断日期
        if isDateInThisYear(targetDate: newDate) {
            if calendar.isDateInToday(newDate) {
                //创建当前日期对象
                let date = Date()
                let detla =  date.timeIntervalSince(newDate)
                if detla < 60 {
                    return "刚刚"
                } else if detla < 3600 {
                    //分钟前  去掉小数部分
                    return "\(Int(detla) / 60)分钟前"
                } else {
                    //小时前
                    return "\(Int(detla) / 3600)小时前"
                }
            } else if calendar.isDateInYesterday(newDate) {
                //昨天
                sharedDateFormater.dateFormat = "昨天 HH:mm"
                return sharedDateFormater.string(from: newDate)
            } else {
                //今年内的其他时间
                sharedDateFormater.dateFormat = "MM-dd"
                return sharedDateFormater.string(from: newDate)
            }
        } else {
            //非今年
            sharedDateFormater.dateFormat = "yyyy-MM-dd"
            return sharedDateFormater.string(from: newDate)
        }
    }
    var status : LKStatus? {
        didSet{
            //设置url
            setIconURL()
            //设置认证图标
            setAvatarImage()
            //设置会员图标
            setVipImage()
            //设置底部工具栏标题
//            setCommentText()
//            setRepostText()
//            setLikeText()
            comment_text = setBtnText(defaltText: "评论", count: status?.comments_count ?? 0)
            repost_text = setBtnText(defaltText: "转发", count: status?.reposts_count ?? 0)
            like_text = setBtnText(defaltText: "赞", count: status?.attitudes_count ?? 0)
            repostContentText = status?.retweeted_status?.text
            sourceText = dealSource()
        }
        
    }
    private func dealSource () -> String {
        let str = status?.source ?? ""
        //定义开始标记
        let startFlag = "\">"
        //定义结束标记
        let endFlag = "</a>"
        //查找标记的所在的位置
        guard let startRangeIndex = str.range(of: startFlag),
            let endRangeIndex = str.range(of: endFlag) else {
                return "来自火星"
        }
        //获取拼接的范围
        //upperBound 从范围的后面开始
        let startIndex = startRangeIndex.upperBound
        //lowerBound 从范围的前面开始
        let endIndex = endRangeIndex.lowerBound
        
        let rangeIndex = startIndex..<endIndex
        let subStr = str.substring(with: rangeIndex)
        
        return subStr
    }
    private func setIconURL (){
        let urlString = status?.user?.avatar_large ?? ""
        let url = URL(string: urlString)
        iconURL = url
    }
    private func setAvatarImage () {
        switch status?.user?.verified_type ?? -1 {
        case 0:
            avatarImage = #imageLiteral(resourceName: "avatar_vip")
        case 220:
            avatarImage = #imageLiteral(resourceName: "avatar_grassroot")
        case 2,3,5:
            avatarImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
        default:
            avatarImage = nil
        }
    }
    private func setVipImage () {
        if let mbrank = status?.user?.mbrank,mbrank > 0 && mbrank < 7{
            let imageName = "common_icon_membership_level\(mbrank)"
            vipImage = UIImage(named: imageName)
        }
    }
//    private func setCommentText () {
//        let string = (status?.comments_count == 0) ? "评论" : status?.comments_count.description
//        comment_text = string
//    }
//    private func setRepostText () {
//        let string = (status?.reposts_count == 0) ? "转发" : status?.reposts_count.description
//        repost_text = string
//    }
//    private func setLikeText () {
//        let string = (status?.attitudes_count == 0) ? "赞" : status?.attitudes_count.description
//        like_text = string
//    }
    private func setBtnText (defaltText: String,count:Int) -> String {
        if count == 0 {
            return defaltText
        }
        if count >= 10000 {
            return "\(Double(count / 1000) / 10)万"
        }
        return count.description
    }
    private func isDateInThisYear(targetDate: Date) -> Bool {
        let currentDate = Date()
        //重新设置日期格式符
        sharedDateFormater.dateFormat = "yyyy"
        //如果创建的日期转换不成工就直接return一个空串
        //获取创建的年份
        let creatYear = sharedDateFormater.string(from: targetDate)
        //获取当前的年份
        let currentYear = sharedDateFormater.string(from: currentDate)
        //返回比较的结果
        return currentYear == creatYear
    }
}
